package com.yuke.cloud.service.smc.controller.rpc;

import com.alibaba.fastjson.JSON;
import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.api.smc.dto.*;
import com.yuke.cloud.service.api.smc.service.SmcFeignApi;
import com.yuke.cloud.service.smc.entity.DmsSend;
import com.yuke.cloud.service.smc.entity.Shop;
import com.yuke.cloud.service.smc.entity.ShopGoods;
import com.yuke.cloud.service.smc.service.DmsSendService;
import com.yuke.cloud.service.smc.service.DmsStoreService;
import com.yuke.cloud.service.smc.service.ShopGoodsService;
import com.yuke.cloud.service.smc.service.ShopService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Api(value = "WEB - SmcSpecFeignClient", tags = "处理Smc相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
public class SmcFeignClient implements SmcFeignApi {

    @Resource
    private ShopGoodsService shopGoodsService;
    @Resource
    private ShopService shopService;
    @Resource
    private DmsSendService dmsSendService;
    @Resource
    private DmsStoreService dmsStoreService;

    @Async
    @Override
    public Integer updateStockCount(@RequestParam("json") String json) {
        Integer count = 0;
        List<Object> list = JSON.parseArray(json);

        for (Object object : list) {
            Map updateStock = (Map)object;
            Long shopId = Long.valueOf(updateStock.get("shopId").toString());
            Long specId = Long.valueOf(updateStock.get("specId").toString());
            Integer goodsNum = Integer.valueOf(updateStock.get("goodsNum").toString());
            count += shopGoodsService.updateStockCount(shopId, specId, goodsNum);
        }
        return count;
    }

    @Override
    public Byte getGoodsStatus(@RequestParam("shopId") Long shopId, @RequestParam("specId") Long specId) {
        return shopGoodsService.getGoodsStatus(shopId, specId);
    }

    @Override
    public List<GoodsAndSpecInfoDto> getGoodsStatusList(@RequestBody List<GoodsAndSpecInfoDto> goodsAndSpecInfoDtos) {
        return shopGoodsService.getGoodsStatusList(goodsAndSpecInfoDtos);
    }

    @Override
    public Integer getLowPriceBySpecId(@RequestParam("specId")  Long specId) {
        return shopGoodsService.getLowPriceBySpecId(specId);
    }

    @Override
    public List<ShopCartInfoDto> getShopCartInfoDto(@RequestBody List<GoodsAndSpecInfoDto> goodsAndSpecInfoDtos) {
        if (goodsAndSpecInfoDtos.size()<=0) {
            return null;
        }
        return shopService.getShopCartInfoDto(goodsAndSpecInfoDtos);
    }

    @Override
    public List<ShopInfoDto> getShopUserIdByShopIds(@RequestBody List<Long> shopIds) {
        return shopService.getShopUserIdByShopIds(shopIds);
    }

    @Override
    public List<ShopInfoDto> getShopInfoByShopIds(@RequestBody List<Long> shopIds) {
        List<ShopInfoDto> shopInfos = new ArrayList<>();
        if (shopIds == null || shopIds.size() == 0) {
            return shopInfos;
        }
        shopInfos = shopService.getShopInfoByShopIds(shopIds);
        return shopInfos;
    }

    @Override
    public List<Long> getShopIdsShopName(@RequestParam("shopName") String shopName) {
        Shop shop= new Shop();
        shop.setShopName(shopName);
        List<Shop> shopList = shopService.getListByCondition(shop);
        return shopList.stream().map(Shop::getShopId).collect(Collectors.toList());
    }

    @Override
    public Wrapper<List<GoodsAndSpecInfoDto>> getShopGoodsSalePrice(@RequestBody ShopGoodsSalePriceDto shopGoodsSalePriceDto) {
        return WrapMapper.ok(shopGoodsService.getShopGoodsSalePrice(shopGoodsSalePriceDto));
    }

    @Override
    public List<ShopInfoDto> getShopAccountByShopIds(@RequestBody List<Long> shopIds) {
        log.info("--- Enter getShopAccountByShopIds(List<Long> shopIds)，参数：{} ---", shopIds);
        List<ShopInfoDto> shopInfos = new ArrayList<>();
        if (shopIds == null || shopIds.size() == 0) {
            return shopInfos;
        }
        shopInfos = shopService.getShopAccountByShopIds(shopIds);
        log.info("--- Leave getShopAccountByShopIds(List<Long> shopIds)，返回值：{} ---", shopInfos);
        return shopInfos;
    }

    // add by wg 20190329 增加店铺商品库存量的获取
    @Override
    public Integer getShopStockCount(@RequestParam("shopId") Long shopId, @RequestParam("specId") Long specId) {
        log.info("--- Enter getShopStockCount，参数：shopId={}，specId={} ---", shopId, specId);
        ShopGoods shopGoods = new ShopGoods();
        shopGoods.setShopId(shopId);
        shopGoods.setProductId(specId);
        List<ShopGoods> shopGoodsList = shopGoodsService.select(shopGoods);
        Integer count = 0;
        if (shopGoodsList != null && shopGoodsList.size() > 0) {
            count = shopGoodsList.get(0).getStockCount();
        }
        log.info("--- Leave getShopStockCount，返回值：{} ---", count);
        return count;
    }

    // add by wg 20190516
    @Override
    public Wrapper<ShopInfoDto> getShopByFactoryId(@RequestParam("factoryId") Long factoryId) {
        log.info("--- Enter getShopByFactoryId，参数：factoryId={} ---", factoryId);
        ShopInfoDto shopInfoDto = shopService.getShopByFactoryId(factoryId);
        log.info("--- Leave getShopStockCount，返回值：{} ---", shopInfoDto);
        return WrapMapper.ok(shopInfoDto);
    }

    @Override
    public List<ShopInfoDto> getShopIdsByCondition() {
        Shop shop = new Shop();
        shop.setRunStatus((byte)1);
        shop.setCreateStatus((byte)2);
        return shopService.getShopIdsByCondition(shop);
    }

    @Override
    public List<ShopInfoDto> getShopInfoDtoByUserId(@RequestParam("userId") Long userId) {
        List<ShopInfoDto> shopInfos = new ArrayList<>();
        if (userId == null ) {
            return shopInfos;
        }

        shopInfos = shopService.getShopInfoDtoByUserId(userId);
        return shopInfos;
    }

    //根据店铺名获取店铺信息
    @Override
    public List<ShopInfoDto> getShopInfoByShopName(@RequestParam("shopName") String shopName) {
        return shopService.getShopInfoByShopName(shopName);
    }

    @Override
    public List<ShopInfoDto> getShopInfoByGroupId(@RequestParam("groupId") Long groupId) {

        return shopService.getShopInfoByGroupId(groupId);
    }

    @Override
    public List<ShopInfoDto> getShopInfoDtoByUserIdList(@RequestBody List<Long> userIds) {

        return shopService.getShopInfoDtoByUserIdList(userIds);
    }

    @Override
    public Integer addShop(@RequestBody ShopInfoDto shopInfoDto) {
        //保存店铺信息，且绑定仓库和店铺
        Integer num = shopService.saveShop(shopInfoDto);
        return num;
    }

    @Override
    public Integer saveShop(@RequestBody ShopInfoDto shopInfoDto) {
        Shop shop = new Shop();
        //保存店铺信息,只保存店铺信息，和上面的区别
        BeanUtils.copyProperties(shopInfoDto,shop);
        shop.setTelephone(shopInfoDto.getMobile());
        shop.setCreateStatus((byte) 2);
        shop.setRunStatus((byte) 1);
        shop.setOpenTime(480);
        shop.setCloseTime(1380);
        Integer num = shopService.insertShop(shop);

        return num;
    }

    @Override
    public Integer addDmsSend(@RequestParam("userId") Long userId, @RequestParam("orderId") Long orderId, @RequestParam("amountPay") Integer amountPay) {
        DmsSend dmsSend = new DmsSend();
        dmsSend.setAmountPay(amountPay);
        dmsSend.setOrderId(orderId);
        dmsSend.setUserId(userId);
        dmsSend.setPayType((byte)0);
        dmsSend.setSendStatus((byte)0);
        dmsSend.setSendType((byte)1);
        Integer num = dmsSendService.save(dmsSend);
        return num;
    }

    @Override
    public ShopInfoDto getShopInfoByShopId(@RequestParam("shopId") Long shopId) {
        ShopInfoDto shopInfoDto = shopService.getShopInfoByShopId(shopId);
        return shopInfoDto;
    }

    @Override
    public List<ShopInfoDto> getAllShopInfo() {
        List<ShopInfoDto>  list = shopService.getAllShopInfo();
        return list;
    }

    @Override
    public Integer updateSendDeleteTag(Long orderId) {
        return dmsSendService.updateSendDeleteTag(orderId);
    }

    @Override
    public List<ShopInfoDto> getShopNameByShopIds(@RequestBody List<Long> shopIds) {
        return shopService.findShopNameByShopIds(shopIds);
    }


/*
    @Override
    public Shop addShopReturnId(@RequestBody ShopInfoDto shopInfoDto) {
        Shop shop = new Shop();

        BeanUtils.copyProperties(shopInfoDto,shop);
        shop.setTelephone(shopInfoDto.getMobile());
        Integer num = shopService.saveShop(shop);
        return shop;
    }
*/



}
